package com.sunday.fangdaifu.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.activity.main.MainActivity;
import com.sunday.fangdaifu.base.MyApplication;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.common.ApiServiceImpl;
import com.sunday.fangdaifu.common.Const;
import com.sunday.fangdaifu.model.Member;
import com.sunday.fangdaifu.utils.SharePerferenceUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yun_sheng on 2017/6/28.
 */

public class LoginActivity extends BaseActivity implements ApiServiceImpl.HttpResponseInterface{
    @Bind(R.id.edt_username)
    EditText edtUsername;
    @Bind(R.id.edt_password)
    EditText edtPassword;
    @Bind(R.id.see)
    TextView see;
    @Bind(R.id.btn_login)
    TextView btnLogin;
    @Bind(R.id.forget_pwd)
    TextView forgetPwd;
    private String usernName;
    private String userPwd;
    private Context mContext;
    private boolean isSee = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivity_login);
        ButterKnife.bind(this);
        mContext=this;
    }
    @OnClick({R.id.btn_login, R.id.forget_pwd, R.id.see})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                usernName = edtUsername.getText().toString().trim();
                userPwd = edtPassword.getText().toString().trim();
                showLoadingDialog();
                ApiClient.getApiService().login(1,usernName,userPwd,this,new TypeToken<ResultDO<Member>>(){}.getType());
                break;
            case R.id.forget_pwd:
                Intent intent = new Intent(mContext, ForgetPwdActivity.class);
                startActivity(intent);
                break;
            case R.id.see:
                see.setSelected(!isSee);
                isSee = !isSee;
                if (isSee) {
                    edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                }

                break;
        }
    }

    @Override
    public void onResponce(String key, Object result) {
        if (isFinish){
            return;
        }
        dissMissDialog();
        ResultDO<Member> resultDO = (ResultDO<Member>) result;
        if (resultDO.getCode() == 0) {
            SharePerferenceUtils.getIns().putString(Const.USERNAME, usernName);
            SharePerferenceUtils.getIns().putString(Const.PASSWORD, userPwd);
            SharePerferenceUtils.getIns().putBoolean(Const.IS_LOGIN, true);
//            SharePerferenceUtils.getIns().putString(Const.RECORD_MEMBERKEY_KEY, resultDO.getResult().getMemberKey());
            SharePerferenceUtils.getIns().putLong(Const.RECORD_MEMBER_ID, resultDO.getResult().getId());
            MyApplication.getInstance().setMembe(resultDO.getResult());
            SharePerferenceUtils.getIns().saveOAuth(resultDO.getResult());
            Intent intent=new Intent(mContext,MainActivity.class);
            startActivity(intent);
            finish();
        }else {
        ToastUtils.showToast(mContext, "登陆失败，密码或账号不正确");
    }
    }

    @Override
    public void onFailure() {
        dissMissDialog();
        ToastUtils.showToast(mContext,"服务器异常");
    }
}
